package com.data.dao;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.base.dao.DataTemplateDaoImpl;
import com.data.entity.BaseDataDto;
@Repository("baseDataDtoDao")
public class BaseDataDtoDaoImpl extends DataTemplateDaoImpl<BaseDataDto, Long> implements BaseDataDtoDao{
	/**
	 * 查询baseDataDto表中前15条记录
	 * @param equNo
	 * @return
	 */
	@Override
	public List<BaseDataDto> getBaseDataDtoTopN(String equNo) {
		String sql="select top 15 * from baseDataDto where equNo=:equNo and P_engry is not null order by Time desc";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("equNo", equNo);
		return jt.query(sql, params, ParameterizedBeanPropertyRowMapper.newInstance(BaseDataDto.class));
	}
    /**
     * 获取最后一条设备实时数据
     * @param equNo
     * @return
     */
	@Override
	public BaseDataDto getLastBaseDataDto(String equNo) {
		String sql="select top 1 * from baseDataDto where equNo=:equNo and P_engry is not null order by time desc ";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("equNo", equNo);
		return jt.queryForObject(sql, params, 
				ParameterizedBeanPropertyRowMapper.newInstance(BaseDataDto.class));
	}
	/**
	 * 获取某一具体时间点的记录，如果没有返回离改时间点最近的记录
	 * @param equNo
	 * @param date
	 * @return
	 */
	@Override
	public BaseDataDto getBaseDataDtoByEquNoAndDate(String equNo, String date) {
		String sql="select top 1 *,abs(DATEDIFF(SS,CONVERT(VARCHAR(19),Time,120),:date)) as ts from baseDataDto  where equNo=:equNo and P_engry is not null  order by ts";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("equNo", equNo);
		params.put("date", date);
		return jt.queryForObject(sql, params, ParameterizedBeanPropertyRowMapper.newInstance(BaseDataDto.class));
	}
	/**
	 * 定时删除baseDataDto表中的数据
	 * @param date
	 */
	@Override
	public void deleteBaseDataDto(String date) {
    	String sql="delete from baseDataDto where CONVERT(VARCHAR(10),Time,120)<=:date";
    	Map<String, Object> params=new HashMap<String, Object>();
		params.put("date", date);
		jt.update(sql, params);
	}
}
